<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>函数柯里化</title>
  </head>
  <body>
    <script>
      function currying(fn) {
        var argsLen = fn.length;
        function createFnc(curriedArgs) {
          return function () {
            var newCurriedArgs = curriedArgs.concat(
              Array.prototype.slice.call(arguments)
            );
            if (newCurriedArgs.length === argsLen) {
              return fn.apply(this, newCurriedArgs);
            } else {
              return createFnc(newCurriedArgs);
            }
          };
        }
        return createFnc([]);
      }
      function add3params(a, b, c) {
        return a + b + c;
      }
      const res = currying(add3params);
      console.log(res(1, 2, 3));
      console.log(res(1)(2, 3));
      console.log(res(1)(2)(3));
    </script>
  </body>
</html>
